/* 
 * Quechua - the lightweight data mining framework
 *
 * Copyright (C) 2012 Marek Denis <quechua@octogan.net>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "../include/interface-channel.h"
#include "../include/quechua.h"

Channel::Channel() : internal(new Internal) {
    init();
}

Channel::~Channel() {
    LOG(INFO) << "Destroying Channel " << internal->name;
    workflows.clear(); 
}

void Channel::init() {
    if(internal==NULL) {
        LOG(CRITIC) << "Channel internal is not initialized, exiting";
        return;
    }
    internal->type = CHANNEL;
};

// let me repeat myself again - I HATE exceptions in C++.
bool Channel::record(Workflow* component) {
    bool result = true;
    try {
        workflows.push_back(component);
    } catch (alloc_ex& ex) {
        LOG(ERROR) << ex.what();
        result = false;
    }
    return result;
}

bool Channel::unrecord(Workflow* component) {
    if(workflows.empty()) return false;
    workflows.remove(component);
    return true;
};

bool Channel::load_misc_conf(const setting_t& misc) {
    return true;
}

bool Channel::prepare() { 
    return true;
}

bool Channel::start() {
    return true;
}

bool Channel::stop() {
    return true;
};

bool Channel::reset() {
    return true;
};

bool Channel::notify() {
    return true;
};

void Channel::notify_workflows(DataPack* data_chunk) {

    datapack_ptr ptr(data_chunk);

    workflows_list::iterator it = workflows.begin();
    workflows_list::iterator end= workflows.end();
    for(it;it!=end;++it){
        (*it)->notify(ptr,IDLE);
    }
}

